package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.HotQuestion;
import com.jyy.prefabricated.entity.News;
import com.jyy.prefabricated.service.HotQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 热门问题
 * @version 1.0.0
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class HotQuestionController {

    @Autowired
    private HotQuestionService hotQuestionService;

    /**
     * 新增热门问题
     * @param hotQuestion
     * @return
     */
    @PostMapping("/hot-question")
    public AjaxResult insert(@Valid @RequestBody HotQuestion hotQuestion){
        log.info("Rest request to insert hot-question:{}",hotQuestion);
        int id = hotQuestionService.insert(hotQuestion);
        HotQuestion result = hotQuestionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改热门问题
     * @param hotQuestion
     * @return
     */
    @PutMapping("/hot-question")
    public AjaxResult update(@Valid @RequestBody HotQuestion hotQuestion){
        log.info("Rest request to update hot-question:{}", hotQuestion);
        hotQuestionService.update(hotQuestion);
        HotQuestion result = hotQuestionService.selectById(hotQuestion.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量修改人吗问题状态
     * @param dto
     * @return
     */
    @PutMapping("/hot-question-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update hot-question:{}",dto);
        hotQuestionService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除热门问题
     * @param dto
     * @return
     */
    @DeleteMapping("/hot-question-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete hot-question by id:{}",dto);
        int row = hotQuestionService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询热门问题详情
     * @param id
     * @return
     */
    @GetMapping("/hot-question/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find hot-question by id:{}",id);
        HotQuestion result = hotQuestionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除热门问题
     * @param id
     * @return
     */
    @DeleteMapping("/hot-question/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete hot-question by id:{}",id);
        int row = hotQuestionService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询上一篇热门问题
     * @param id
     * @return
     */
    @GetMapping("/hot-question-prev/{id}")
    public AjaxResult selectByPrev(@PathVariable Integer id){
        log.info("Rest request to find hot-question by prev id:{}",id);
        List<HotQuestion> result = hotQuestionService.selectByPage(id,"prev");
        return AjaxResult.success(result);
    }

    /**
     * 查询下一篇热门问题
     * @param id
     * @return
     */
    @GetMapping("/hot-question-next/{id}")
    public AjaxResult selectByNext(@PathVariable Integer id){
        log.info("Rest request to find hot-question by next id:{}",id);
        List<HotQuestion> result = hotQuestionService.selectByPage(id,"next");
        return AjaxResult.success(result);
    }

    /**
     * 分页查询热门问题
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @param top 是否置顶
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/hot-question")
    public AjaxResult selectByNext(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status,@RequestParam(required = false) Boolean top, Pageable pageable){
        log.info("Rest request to find hot-question by params categoryId:{}, title:{}, status:{},top:{}",categoryId, title, status,top);
        List<Integer> categoryIds = hotQuestionService.selectDeepCategoryIds(categoryId);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<HotQuestion> news = hotQuestionService.selectDeepByParams(categoryIds, title, status, top);
        PageInfo<HotQuestion> result = new PageInfo<>(news);
        return AjaxResult.success(result);
    }

    /**
     * 查询热门问题
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @param top 是否置顶
     * @return
     */
    @GetMapping("/hot-question-list")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status,@RequestParam(required = false) Boolean top){
        log.info("Rest request to find hot-question by params categoryId:{}, title:{}, status:{},top:{}",categoryId, title, status,top);
        List<Integer> categoryIds = hotQuestionService.selectDeepCategoryIds(categoryId);
        List<HotQuestion> result = hotQuestionService.selectDeepByParams(categoryIds, title, status, top);
        return AjaxResult.success(result);
    }
}
